package com.ccp.dev.workflow.service.bpm;

import com.ccp.dev.core.basic.api.ISysUser;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.model.MessageModel;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.workflow.bpmconst.BpmConst;
import com.ccp.dev.workflow.bpmutil.BpmUtil;
import com.ccp.dev.workflow.bpmutil.ServiceUtil;
import com.ccp.dev.workflow.model.BpmNodeMessage;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.service.BpmNodeMessageService;
import com.ccp.dev.workflow.service.ProcessRunService;
import com.ccp.dev.workflow.service.jms.MessageProducer;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassDescribe:
 * 发送消息任务
 * @author :wangcheng
 * Date: 2019-08-06
 */
@Service
public class MessageTask implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        sendMessage(execution);
    }

    private void sendMessage(DelegateExecution execution) {
        BpmNodeMessageService bpmNodeMessageService = AppUtil.getBean(BpmNodeMessageService.class);
        ProcessRunService processRunService = AppUtil.getBean(ProcessRunService.class);

        String procDefId = execution.getProcessDefinitionId();
        String nodeId = execution.getCurrentActivityId();
        String procInstId = execution.getProcessInstanceId();
        Map<String, Object> variables = execution.getVariables();
        ProcessRun processRun = processRunService.getByProcInstanceId(procInstId);
        String runId = processRun.getRunId();
        String subject = processRun.getSubject();
        SysUser currentUser = (SysUser) ContextUtil.getCurrentUser();
        if (BeanUtils.isEmpty(currentUser)) {
            currentUser = new SysUser();
            currentUser.setFullName("系统");
            currentUser.setUserId("0");
        }
        List<BpmNodeMessage> bpmNodeMessages = bpmNodeMessageService.getNodeMessage(procDefId, nodeId);

        for (BpmNodeMessage message : bpmNodeMessages) {
            if (BeanUtils.isEmpty(message) || message.getIsSend()==0) {
                continue;
            }
            Short messageType = message.getMessageType();
            if (messageType.equals(BpmNodeMessage.MESSAGE_TYPE_INNER)) {
                sendInnerMessage(execution, message, currentUser, subject, runId, variables);
            } else if (messageType.equals(BpmNodeMessage.MESSAGE_TYPE_MAIL)) {
                sendMailMessage(execution, message, currentUser, subject, runId, variables);
            } else if (messageType.equals(BpmNodeMessage.MESSAGE_TYPE_SMS)) {
                sendSmsMessage(execution, message.getTemplate(), currentUser, subject, variables);
            }
        }
    }

    /**
     * 发送手机短信
     * @param execution 流程实例
     * @param template 消息模板
     * @param currentUser 当前用户
     * @param subject 标题
     * @param variables 流程变量
     */
    private void sendSmsMessage(DelegateExecution execution, String template, SysUser currentUser, String subject, Map<String, Object> variables) {
        System.out.println("我是发送手机短信==========");
    }

    /**
     *  发送邮件
     * @param execution 流程实例
     * @param message 消息
     * @param currentUser 当前用户
     * @param subject 标题
     * @param runId 流程封装类id
     * @param variables 流程变量
     */
    private void sendMailMessage(DelegateExecution execution, BpmNodeMessage message, SysUser currentUser, String subject, String runId, Map<String, Object> variables) {
        System.out.println("我是发送邮件信息");
    }

    /**
     * 发送站内消息
     * @param execution 流程实例
     * @param message 消息
     * @param currentUser 当前用户
     * @param title 标题
     * @param runId 流程封装类id
     * @param variables 流程变量
     */
    private void sendInnerMessage(DelegateExecution execution, BpmNodeMessage message, SysUser currentUser, String title, String runId, Map<String, Object> variables) {
        MessageProducer messageProducer = AppUtil.getBean(MessageProducer.class);
        BpmNodeMessageService bpmNodeMessageService = AppUtil.getBean(BpmNodeMessageService.class);
        List<SysUser> userList = bpmNodeMessageService.getInnerReceiver(execution, currentUser.getUserId());
        if (BeanUtils.isEmpty(userList)) {
            return;
        }
        for (SysUser user : userList) {
            String url = ServiceUtil.getUrl(runId, false);
            //主题
            String subject = ServiceUtil.replaceTitleTag(message.getSubject(), user.getFullName(), currentUser.getFullName(), title, "");
            //发送内容,无模版就取发送内容
            String content = ServiceUtil.replaceTemplateTag(message.getTemplate(), user.getFullName(), currentUser.getFullName(), title, url, "", false);
            content = BpmUtil.getTitleByRule(content, variables);

            //消息发送模型统一用MessageModel
            MessageModel messageModel=new MessageModel(BpmConst.MESSAGE_TYPE_INNER);
            messageModel.setSubject(subject);
            messageModel.setContent(content);
            messageModel.setSendDate(new Date());
            messageModel.setReceiveUser(user);
            messageModel.setSendUser(currentUser);
            messageProducer.send(messageModel);

        }
        System.out.println("我是发送站内消息");
    }
}
